package com.lw.extraceim.util;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;

import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;

/**
 * zxing的工具类
 */
public class ZxingUtil {


    /**
     * 启动条形码扫描
     *
     * @param activity
     * @param captureActivity
     * @param prompt
     * @param requestCode
     */
    public static void startQRScan(Activity activity, Class captureActivity,
                                   String prompt, int requestCode) {
        IntentIntegrator intentIntegrator = new IntentIntegrator(activity);
        intentIntegrator.setCaptureActivity(captureActivity);
        intentIntegrator.setDesiredBarcodeFormats(IntentIntegrator.ONE_D_CODE_TYPES);
        intentIntegrator.setPrompt(prompt);
        //设置请求码
        intentIntegrator.setRequestCode(requestCode);
//        application.setScanActivityRequestCode(requestCode);
        intentIntegrator.setBeepEnabled(true);
        intentIntegrator.setBarcodeImageEnabled(true);
        intentIntegrator.initiateScan();
    }

    /**
     * 获取扫描的结果
     *
     * @param resultCode
     * @param data
     * @return
     */
    public static String scanResult(int resultCode, Intent data) {
        IntentResult scanResult = IntentIntegrator.parseActivityResult(IntentIntegrator.REQUEST_CODE, resultCode, data);
        if (scanResult != null) {
            return scanResult.getContents();
        }
        return null;
    }




}
